<!--
 * @description:
 * @author: cuicui
 * @Date: 2023-11-30 15:52:56
 * @LastEditTime: 2023-11-30 15:58:47
 * @LastEditors: cuicui
-->
<template>
  <a-modal
    :title='currentTitle'
    :width='640'
    :visible='visible'
    :confirmLoading='loading'
    v-bind='canEdit ? {} : { footer: null}'
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning='loading'>
      <a-form :form='form' v-bind='formLayout'>
        <a-form-item label='主键' v-show='false'>
          <a-input v-decorator="['id']"></a-input>
        </a-form-item>

        <a-form-item label='算法任务名称'>
          <a-input v-show='canEdit' v-decorator="[
              'taskname',
              { rules: [{ required: true, message: '请输入任务名称!' },
             { pattern: namePattern,  message: '请输入合法内容(可输入0到50位（中文、字母，数字，下划线，减号)'}] },
            ]"></a-input>
          <div v-show='!canEdit' class='view-item'>{{ model && model.taskname }}</div>
        </a-form-item>

        <a-form-item label='模块'>
          <a-select v-show='canEdit' v-decorator="[
              'moduleDicId',
              { rules: [{ required: true, message: '请选择所属模块!' }] },
            ]" :options='moduleList'>
          </a-select>
          <div v-show='!canEdit' class='view-item'>{{ model && model.moduleName }}</div>
        </a-form-item>


        <a-form-item label='算法'>
          <a-select v-show='canEdit' v-decorator="[
              'algorithmId',
              { rules: [{ required: true, message: '请选择算法!' }] },
            ]" :options='algorithmList'>
          </a-select>
          <div v-show='!canEdit' class='view-item'>{{ model && model.algorithmName }}</div>
        </a-form-item>

        <a-form-item label='依赖算法'>
          <a-select v-show='canEdit' mode='multiple' v-decorator="[
              'dependOnAlgorithm'
            ]" :options='algorithmList'>
          </a-select>
          <div v-show='!canEdit' class='view-item'>
            <div v-if='model && model.dependOnAlgorithmNames'>
              <a-tag v-for='(item, index) in model.dependOnAlgorithmNames' :key='index'>
                {{ item }}
              </a-tag>
            </div>
          </div>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { mapState } from 'vuex'
import { findAlgorithmByName } from '@/api/algorithmConfig'
import { namePattern } from '@/reges'
// 表单字段
const fields = ['id', 'taskname', 'moduleDicId', 'algorithmId', 'dependOnAlgorithm']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    canEdit: {
      type: Boolean,
      default: () => true
    }
  },
  computed: {
    ...mapState({
      moduleList: state => state.dataDicInfo.modules.map(item => {
        return {
          key: item.id,
          label: item.name
        }
      })
    }),
    currentTitle() {
      if (!this.canEdit) {
        return '算法任务详情'
      }
      return (this.model ? '编辑' : '新增') + '算法任务'
    }
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      }
    }
    return {
      namePattern,
      algorithmList: [],
      form: this.$form.createForm(this)
    }
  },
  created() {
    this.findAlgorithmByName()
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      fields.forEach(v => this.form.getFieldDecorator(v))
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    async findAlgorithmByName() {
      const result = await findAlgorithmByName()
      this.algorithmList = result.map(item => {
        return {
          ...item,
          key: item.id,
          label: item.algorithmName
        }
      })
    }
  }
}
</script>
